package com.asktgapp.user.fragment;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.asktgapp.R;
import com.asktgapp.application.MyApplication;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.hxsg.BiuEaseHelper;
import com.asktgapp.modulebase.common.util.PreferencesUtil;
import com.hyphenate.EMConnectionListener;
import com.hyphenate.EMError;
import com.hyphenate.chat.EMClient;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * time:2018/2/1
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:
 */

public class ConsultFragment extends BaseFragment {
    @InjectView(R.id.group)
    RadioGroup mRadioGroup;
    @InjectView(R.id.viewpager)
    ViewPager mViewPager;

    @InjectView(R.id.myBack)
    TextView myBack;
    @InjectView(R.id.myToolbarRight)
    TextView myToolbarRight;
    private MyPagerAdapter mAdapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_consult, container, false);
        ButterKnife.inject(this, viewGroup);

        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().getToolbar().setVisibility(View.GONE);
        mAdapter = new MyPagerAdapter(getChildFragmentManager());
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(0);
        mViewPager.setOffscreenPageLimit(2);
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                int position = group.indexOfChild(group.findViewById(checkedId));
                mViewPager.setCurrentItem(position);
            }
        });

        myBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getBaseActivity().onBackPressed();
            }
        });
//        if (PreferencesUtil.getBoolean(getActivity(), PreferencesUtil.KEY_IS_ENGINEER)) {
//            PreferencesUtil.putString(getActivity(), PreferencesUtil.KEY_HX_ID, PreferencesUtil.getString(getActivity(), PreferencesUtil.KEY_PHONE));
//            BiuEaseHelper.loginIn();
//        } else {
////            if (!Util.isEmpty(PreferencesUtil.getString(getActivity(), PreferencesUtil.KEY_HX_ID))) {
//            BiuEaseHelper.loginIn();
////                return;
////            }else {
////
////            }
//
//        }

        if (!MyApplication.userIdIsEmpty(getContext())) {
            EMConnectionListener connectionListener = new EMConnectionListener() {
                @Override
                public void onConnected() {
                }

                @Override
                public void onDisconnected(final int error) {
//                getActivity().runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        doDisconnected(error);
//                    }
//                });
//
                }
            };
            EMClient.getInstance().addConnectionListener(connectionListener);

        }

    }

    public void doDisconnected(int error) {
        if (error == EMError.USER_REMOVED) {

        } else if (error == EMError.USER_LOGIN_ANOTHER_DEVICE) {
//            Toast.makeText(this, "帐号在其他设备登录", Toast.LENGTH_SHORT).show();
        } else if (error == EMError.SERVER_SERVICE_RESTRICTED) {
            BiuEaseHelper.loginOut();
        } else {
            BiuEaseHelper.reset();
        }
    }

    @Override
    public void loadData() {

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();

        ButterKnife.reset(this);
    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    if (PreferencesUtil.getBoolean(getActivity(), PreferencesUtil.KEY_IS_ENGINEER)) {
                        return ConsultItemFragment.newInstance(2);

                    } else {
                        return ConsultItemFragment.newInstance(0);
                    }
                case 1:
                    if (PreferencesUtil.getBoolean(getActivity(), PreferencesUtil.KEY_IS_ENGINEER)) {
                        return ConsultItemFragment.newInstance(3);
                    } else {
                        return ConsultItemFragment.newInstance(1);
                    }
                default:
                    return ConsultItemFragment.newInstance(0);
            }

        }

        @Override
        public int getCount() {
            return 2;
        }
    }


    @Override
    public void onResume() {
//        Apiservice apiservice = ApiUtil.getInstance().create();
//        HashMap hashMap = new HashMap();
//        hashMap.put("userid", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
//        hashMap.put("is_type", 0);
//        Call<ApiResponseBody<HxAccountVO>> call = apiservice.chatToUser(hashMap);
//        call.enqueue(new Callback<ApiResponseBody<HxAccountVO>>() {
//            @Override
//            public void onResponse(Call<ApiResponseBody<HxAccountVO>> call, Response<ApiResponseBody<HxAccountVO>> response) {
//                if (response.isSuccessful()) {
//                    HxAccountVO bean = response.body().getResult();
//                    if (Util.isEmpty(bean.getHxName())) {
//                        PreferencesUtil.putString(getActivity(), PreferencesUtil.KEY_HX_ID, PreferencesUtil.getString(getActivity(), PreferencesUtil.KEY_PHONE)+"");
//                    } else
//                        PreferencesUtil.putString(getActivity(), PreferencesUtil.KEY_HX_ID, bean.getHxName());
//                    BiuEaseHelper.loginIn();
//                } else {
//                    Toast.makeText(getActivity(), response.raw().message(), Toast.LENGTH_SHORT).show();
//                    Log.i("jginfo_Mainactivity", response.raw().message());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<ApiResponseBody<HxAccountVO>> call, Throwable t) {
//                ApiException apiException = ExceptionHandle.handleException(t);
////                    Toast.makeText(MainActivity.this, apiException.getMessage(), Toast.LENGTH_SHORT).show();
//                Log.i("jginfo_Mainactivity", apiException.getMessage());
//
//            }
//        });
//        BiuEaseHelper.loginIn();
        super.onResume();
    }
}
